<!-- 在这个文件对每个tab对应的列表进行渲染 -->
<template>
	<view class="trade-list-contetn">
		<!-- 这里设置了z-paging加载时禁止自动调用reload方法，自行控制何时reload（懒加载）-->
		<!--  :enable-back-to-top="currentIndex===tabIndex" 在微信小程序上可以多加这一句，因为默认是允许点击返回顶部的，但是这个页面有多个scroll-view，会全部返回顶部，所以需要控制是当前index才允许点击返回顶部 -->
		<z-paging ref="paging" v-model="dataList" @query="queryList" :fixed="false" :auto="true">
			<template #top>
				<view class="" style="background-color: #F8F8F8;">
					<u-navbar :border-bottom="false" title="订单查询" :background="{'background':'#FFBB1A'}" :title-size="36" :title-bold="true" :title-color="textColor">
						<template #right>
							<view class="u-p-r-20"
							:style="{'color':(StartTime!==''||EndTime!==''||otherShopName!='')?'#ff551a':'#333'}"
							 @click="filterShow = true;">
								<view class="u-flex" style="flex-direction: column;">
									<view class="iconfont icon-shaixuan1"></view>
								</view>
								<view class="u-font-24">筛选</view>
							</view>
						</template>
						
					</u-navbar>
					<view class="u-p-35 u-flex stat-container" style="background-color: #FFBB1A;"  >
						<view class="tab-item" :class="{'tab-item-active':activeTab=== 1}" @click="tabSwitch(1)">采购订单</view>
						<view class="tab-item" :class="{'tab-item-active':activeTab=== 2}" @click="tabSwitch(2)">销售订单</view>
					</view>
				</view>
			</template>
			<!-- 如果希望其他view跟着页面滚动，可以放在z-paging标签内 -->
			<view class="trade-list-container" v-for="(item,index) in dataList" :key="index" @click="goStatDetails(item.id)">
				<view class="payorder-section">
					<view class="section-list">
						<view class="section-item">
							<view class="u-flex u-row-between">
								<view class="u-flex u-m-b-10">
									<view :class="['u-flex', 'u-row-center','u-m-r-20', 'u-col-center','pay-status',{'pay-status-active':item.tradeState>0}]">
										<u-icon name="order" class="u-m-r-10 u-m-b-4" :color="item.tradeState>0?'#FFBB1A':'#999'" size="30"></u-icon>
										<text class="u-font-22" :style="{'color':item.tradeState>0?'#FFBB1A':'#999'}"> {{item.tradeStateName}}</text>
									</view> 
									<view :class="['u-flex', 'u-row-center','u-m-r-20', 'u-col-center','pay-status',{'pay-status-active':item.paymentState!==0}]">
										<img :src="item.paymentState!==0?'static/xiaoshou/zhifudan.png':'static/xiaoshou/zhifuda.png'" style="width: 26rpx;height: 26rpx;" alt="">
										<text class="u-font-22 u-m-l-10" :style="{'color':item.paymentState!==0?'#FFBB1A':'#999'}"> {{item.paymentStateName}}</text>
									</view>
								</view>
							</view>
							<view class="u-flex u-m-t-18">
								<view class="shop-mark">销</view>
								<view class="u-font-28 u-m-l-20">{{item.sellerName}}</view>
							</view>
							<view class="u-flex u-m-t-10 u-m-b-18">
								<view class="shop-mark" style="background-color: #FF551A;">采</view>
								<view class="u-font-28 u-m-l-20">{{item.buyerName}}</view>
							</view>
							<view class="u-flex">
								<view class="section-item-count u-m-b-10 u-m-r-30 u-flex u-col-center" >
									本公司商品 <text class="u-m-l-20 u-font-32" style="color: #333;font-weight: 500;">  {{item.totalOrders}}  </text>
								</view>
								<view class="section-item-count u-m-b-10 u-flex u-col-center" >
									金额合计 <text class="u-m-l-20 u-font-32" style="color: #333;font-weight: 500;">  ￥{{item.totalAmount}}  </text>
								</view>
							</view>
							<view class="u-flex u-row-between">
								<view class="section-item-count u-m-b-10 u-flex u-col-center" >
									下单时间<text class="u-m-l-20 u-font-22" style="color: #999;">  {{$u.timeFormat(item.createdTime, 'yyyy-mm-dd hh:MM')}}  </text>
								</view>
							</view>
						</view>
					</view>
					
				</view>
			</view>
			
		</z-paging>
		<!-- 筛选popup -->
		<u-popup v-model="filterShow" :closeable="true" mode="bottom" @close="confirmTapFn()">
			<view class="box-padding filter-popup"  style="background-color: #FFFFFF;overflow: hidden;">
				<view class="u-text-center u-m-b-20">筛选</view>
				<view class="u-font-30 u-text-bold u-m-b-20" >当前客户</view>
				<view class="" style="border-bottom: 1rpx solid #ececec;" @click="customShow = true;">
					<view class="u-flex u-row-between u-p-t-16 u-p-b-16">
						<text class="u-font-28">{{customName}}</text>
						<u-icon name="arrow-right" :size="'28rpx'"></u-icon>
					</view>
					<!-- <u-input v-model="params.ItemName" placeholder="输入关键字筛选" :type="'text'" :border="false" /> -->
				</view>
				<view class="u-font-30 u-text-bold u-m-t-40 u-m-b-30">交易对象</view>
				<view class="u-m-b-30">
					<view style="border-bottom: 1rpx solid #eaeaea;" >
						<u-input v-model="otherShopName" type="text" :border="false"  placeholder="请输入交易对象名称" />
					</view>
				</view>
				<view class="u-font-30 u-text-bold u-m-t-40 u-m-b-30">交易日期</view>
				<view class="u-flex u-row-between">
					<view class="select-style u-flex u-row-center" @click="StartShow=true">
						<text :style="{color:StartLabel?'#333':'#999'}">{{StartLabel||'起始时间'}}</text>
					</view>
					<view class="date-line"></view>
					<view class="select-style  u-flex u-row-center " @click="EndShow=true">
						<text :style="{color:EndLabel?'#333':'#999'}">{{EndLabel||'结束时间'}}</text>
					</view>
				</view>
				<view class="btn-group u-flex">
					<view class="reset-btn u-flex u-row-center" @click="resetTapFn()">重置</view>
					<view class="confirm-btn u-flex u-row-center" @click="filterShow = false;">确定</view>
				</view>
			</view>
		</u-popup>
		<u-popup v-model="customShow" mode="bottom">
			<view style="min-height: 800rpx;">
				<view class="u-text-center  u-font-32" style="line-height: 100rpx;height: 100rpx;font-weight: bold;">
					选择当前客户
				</view>
				<CustomList :chooseCustom="customId" @getCustom="getCustomFn"></CustomList>
			</view>
		</u-popup>
		<u-picker v-model="StartShow" mode="time" :params="startPicker" @confirm="startTimeChangeFn" @cancel="startTimeCancelFn"></u-picker>
		<u-picker v-model="EndShow" mode="time" :params="startPicker" @confirm="endTimeChangeFn" @cancel="endTimeCancelFn"></u-picker>
	</view>
</template>

<script>
	import CustomList from './customList.vue'
	export default {
		components: {
			CustomList
		},
		data() {
			return {
				//v-model绑定的这个变量不要在分页请求结束中自己赋值！！！
				dataList: [],
				searchText:'',
				activeTab:0,
				customId:"",
				otherShopName:'',
				StartShow:false,
				EndShow:false,
				StartLabel:'',
				StartTime:'',
				EndLabel:'',
				EndTime:'',
				startPicker:{
					year: true,
					month: true,
					day: true,
					hour: true,
					minute: true,
					second: true,
					// 选择时间的时间戳
					timestamp: true
				},
				filterShow:false,
				customShow:false,
				customName:'',
			}
		},
		created(){
			
		},
		onLoad(options) {
			this.customId = options.customId;
			this.customName = options.customName;
			this.activeTab = Number(options.type);
		},
		methods: {
			getCustomFn(val){
				console.log(val);
				this.customId =val.shopId;
				this.customName = val.name;
				this.customShow = false;
			},
			confirmTapFn(){
				this.$refs.paging.reload();
				this.filterShow = false;
			},
			resetTapFn(){
				this.otherShopName = '';
				this.StartTime = '';
				this.EndTime = '';
				this.StartLabel = '';
				this.EndLabel = '';
			},
			startTimeChangeFn(data){
				let dateStr = data.year+'-'+data.month+'-'+data.day+' '+data.hour+':'+data.minute+':'+data.second;
				let dateStr2 = data.year+'-'+data.month+'-'+data.day+' '+data.hour+':'+data.minute;
				this.StartLabel = dateStr2;
				this.StartTime = dateStr;
			},
			startTimeCancelFn(){
				this.StartLabel = '';
				this.StartTime = '';
			},
			endTimeChangeFn(data){
				let dateStr = data.year+'-'+data.month+'-'+data.day+' '+data.hour+':'+data.minute+':'+data.second;
				let dateStr2 = data.year+'-'+data.month+'-'+data.day+' '+data.hour+':'+data.minute;
				this.EndLabel = dateStr2;
				this.EndTime = dateStr;
			},
			endTimeCancelFn(){
				this.EndLabel = '';
				this.EndTime = '';
			},
			tabSwitch(index){
				this.activeTab = index;
				this.reloadFn();
			},
			
			reloadFn(){
				this.$refs.paging.reload();
			},
			goStatDetails(id){
				this.$mRouter.push({
					route: '/pages/owners/tradeList/details?id='+id 
				})
			},
			async queryList(pageNo, pageSize) {
				const params = {
					pageIndex: pageNo,
					pageSize: pageSize,
					CustomerId:this.customId,
					IsSeller:this.activeTab===1?false:true
				}
				if(this.otherShopName!==''){
					params.name = this.otherShopName;
				};
				if(this.StartTime!==''){
					params.BeginTime = this.StartTime;
				};
				if(this.EndTime!==''){
					params.EndTime = this.EndTime;
				}
				uni.showLoading({
					title: 'loading...',
					mask: false
				});
				const res = await this.$newHttp.get('api/BrandOwnerSalesman/GetMyCustomerTradesPage', { params: params } );
				uni.hideLoading({
					title: 'loading...'
				})
				if(res.code === 200){
					this.$refs.paging.complete(res.data.list);
				}else{
					this.$refs.paging.complete(false);
				}
			},
			itemClick(item) {
				console.log('点击了', item.title);
				this.$mRouter.push({ 
					route: '/pages/sale/payorder/createPayOrder?shop='+item.id,
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	@import '@/static/iconfont/iconfont.css';
	/* 注意:父节点需要固定高度，z-paging的height:100%才会生效 */
	.trade-list-contetn {
		height: 100%;
		
	}
	.filter-popup{
		.date-line{
			height: 2rpx;
			width: 24rpx;
			background-color: #757575;
			margin: 0 20rpx;
		}
		.select-style{
			border-radius: 35rpx;
			border: 1rpx solid #e7e7e7;
			font-size: 24rpx;color: #999;
			flex:1;
			height: 70rpx;
		}
		.btn-group{
			margin: 200rpx 0 100rpx;
			.reset-btn{
				flex:1;
				font-size: 28rpx;
				height: 70rpx;
				border-radius: 35rpx 0 0 35rpx;
				background-color: #DDDDDD;
			}
			.confirm-btn{
				background-color: #FFBB1A;
				height: 70rpx;
				flex:1;
				font-size: 28rpx;
				border-radius:0 35rpx 35rpx 0;
			}
		}
	}
	.stat-container{
		.tab-item{
			flex:1;
			text-align: center;
			font-size: 30rpx;
			color: #9A752A;
		}
		.tab-item-active{
			color:#333;
			font-size: 32rpx;
		}
	}
	.trade-list-container{
		background-color: #fff;
		box-sizing: border-box;
		padding: 0 30rpx;
		.payorder-header{
			display: flex;
			justify-content: space-between;
		}
		.payorder-section{
			display: flex;
			align-items: center;
			box-sizing: border-box;
			padding: 35rpx 0;
			margin-bottom: 20rpx;
			.section-image{
				width: 80rpx;
				height: 80rpx;
				border-radius: 50%;
				margin-right: 32rpx;
			}
			.section-list{
				flex: 1;
				margin-left: 20rpx;
				.shop-mark{
					color: #fff;background-color: #FFBB1A;border-radius: 10rpx;width: 36rpx;height: 36rpx;
					text-align: center;
					font-size: 22rpx;
					line-height: 36rpx;
				}
				.section-item{
					width: 100%;
					.pay-status{
						border:1px solid #999;
						box-sizing: border-box;
						padding: 8rpx 18rpx 8rpx 10rpx;
						border-radius: 30rpx;
						color: #999;
					}
					.pay-status-active{
						border-color: #FFBB1A !important;
						color: #FFBB1A;
					}
					.section-item-name{
						font-size: 32rpx;
						font-weight: 500;
						margin-bottom: 10rpx;
					}
					.section-item-count{
						font-size: 24rpx;
						color: #999;
					}
					.section-btn{
						width: 160rpx;
						margin-left: 10rpx;
						height:70rpx;
						border-radius: 35rpx;
						border: 1rpx solid #FFBB1A;
						color: #FFBB1A;
						text-align: center;
						line-height: 70rpx;
					}
					.section-btn-disabled{
						border-color:#999999;
						color: #999999;
					}
				}
			}
			
		}
		
	}


</style>
